package
{
	import flash.display.Sprite;

	public class Car extends Sprite
	{
		private var garageID:int;
		
		private var hood:CarPart = new CarPart("Hood");
		private var body:CarPart = new CarPart("Body");
		private var tail:CarPart = new CarPart("Back");
		private var top:CarPart = new CarPart("Top");
		private var wheel:CarPart = new CarPart("Wheels");
		private var hoodAcs:CarPart = new CarPart("HoodAst");
		
		public var hoodID:int=0;
		public var bodyID:int=0;
		public var tailID:int=0;
		public var topID:int=0;
		public var wheelID:int=0;
		public var hoodAcsID:int=0;
		
		private var xAdjust:int = 50;
		private var yAdjust:int = -20;
		public var gridX:int;
		public var gridY:int;
		private var loadWidth:int = 900;
		public function Car(x:int,y:int)
		{		
			gridX=x;
			gridY=y;
			addChild(tail);
			addChild(wheel);
			addChild(body);
			addChild(hood);
			addChild(hoodAcs);
			addChild(top);
			hood.overrideSize(loadWidth,0);
			body.overrideSize(loadWidth,0);
			tail.overrideSize(loadWidth,0);
			top.overrideSize(loadWidth,0);
			wheel.overrideSize(loadWidth,0);
			hoodAcs.overrideSize(loadWidth,0);
			
			hood.x=garageSubWindow.xAdjust+xAdjust;
			body.x=garageSubWindow.xAdjust+xAdjust;
			tail.x=garageSubWindow.xAdjust+xAdjust;
			top.x=garageSubWindow.xAdjust+xAdjust;
			wheel.x=garageSubWindow.xAdjust+xAdjust;
			hoodAcs.x=garageSubWindow.xAdjust+xAdjust
			
			hood.y=garageSubWindow.yAdjust+yAdjust;
			body.y=garageSubWindow.yAdjust+yAdjust;
			tail.y=garageSubWindow.yAdjust+yAdjust;
			top.y=garageSubWindow.yAdjust+yAdjust;
			wheel.y=garageSubWindow.yAdjust+yAdjust;
			hoodAcs.y=garageSubWindow.yAdjust+yAdjust;
			/*
			body.setColor(0x00FF00);
			hood.setColor(0xFF0000);
			tail.setColor(0xFFFF00);
			top.setColor(0x0000FF);
			hoodAcs.setColor(0xFF00FF);
			wheel.setColor(0x00FFFF);
			*/
			refresh();
		}
		public function setColour(i:uint):void{
			body.setColor(i);
			hood.setColor(i);
			tail.setColor(i);
			top.setColor(i);
			hoodAcs.setColor(i);
			wheel.setColor(0xFFFFFF);
		}
		public function shrink(w:int):void{
			width=w;
			scaleY=scaleX;
		}
		public function grow():void{
			trace("Grow");
			scaleX = 1;
			scaleY=scaleX;
		}
		public function refresh() :void{
			graphics.clear();
			setHood(hoodID);
			setBody(bodyID);
			setTail(tailID);
			setTop(topID);
			setWheel(wheelID);
			setHoodAcs(hoodAcsID);
		}
		private function mod(id:int):int{
			if (id>0)
				id%=CONSTANTS.carTypes;
			if (id<0)id+=CONSTANTS.carTypes;
			return id;
		}
		public function setHood(id:int):void{
			hood.setID(id);
			hoodID=mod(id);
			
		}
		public function setHoodAcs(id:int):void{
			hoodAcs.setID(id);
			hoodAcsID=mod(id);
		}
		public function setBody(id:int):void{
			body.setID(id);
			bodyID=mod(id);
		}
		public function setTail(id:int):void{
			tail.setID(id);
			tailID=mod(id);
		}
		public function setTop(id:int):void{
			top.setID(id);
			topID=mod(id);
		}
		public function setWheel(id:int):void{
			wheel.setID(id);
			wheelID=mod(id);
		}
		public function ts(){
			return ""+tailID+" "+bodyID+" "+hoodID;
		}
		
		public function setAll(st:String):Car{
			var ar = st.split(",");
			setColour(uint(ar[6]));
			hoodID = int(ar[0]);
			hoodAcsID = int(ar[1]);
			bodyID = int(ar[2]);
			tailID = int(ar[3]);
			topID = int(ar[4]);
			wheelID = int(ar[5]);
			return this;
		}
		
	}
}